package com.randy.recyclerviews.utils;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import static com.randy.recyclerviews.utils.Preconditions.checkArgument;
import static com.randy.recyclerviews.utils.Preconditions.checkNotNull;

/**
 * Created by xudshen on 08/08/2017.
 */

public class SafetyVariation {
    @NonNull
    public static <T> T nonNull(@Nullable T obj, @NonNull T defaultObj) {
        Preconditions.checkArgument(defaultObj != null, "defaultObj is null");
        return obj == null ? defaultObj : obj;
    }

    @NonNull
    public static <T> T nonNull(@Nullable T obj, @NonNull NonNullReturn<T> defaultReturn) {
        if (obj != null) return obj;

        checkArgument(defaultReturn != null, "defaultReturn is null");
        obj = defaultReturn.doReturn();
        checkNotNull(obj, "defaultReturn.doReturn() returns with null value");
        return obj;
    }

    interface NonNullReturn<T> {
        @NonNull
        T doReturn();
    }
}
